<?php  
class CartController extends Controller{

	public function __init(){
		//初始化购物车
		if (isset($_SESSION['cart'])) {

			$_SESSION['cart']==array();
		}
	}
	/**
	 * 购物车列表
	 * @return [type] [description]
	 */
	public function index(){
		
		$this->display();
	}
	/**
	 * 添加到购物车
	 */
	public function add(){

		$num=Q('post.num',0,'intval');

		$product_id=Q('post.product_id',0,'intval');

		$gid=Q('post.gid',0,'intval');

		if (!$num || !$gid || !$product_id) {
			$this->error("商品不存在",__ROOT__);
		}
		//获取购物车id
		$cartId=$this->getCartId();

		/**
		 * 当添加的商品在购物车中已经存在时
		 * 只需更改购物车中的货品数量
		 */
		if (isset($_SESSION['cart']['goods'][$cartId])) {
			//修改数量
			$_SESSION['cart']['goods'][$cartId]['num']+=$num;
			//修改总价
			$_SESSION['cart']['goods'][$cartId]['totalPrice']=$_SESSION['cart']['goods'][$cartId]['num']*$_SESSION['cart']['goods'][$cartId]['price'];
		}else{


			$goods=M('goods')->find($gid);

			$product=M('product')->find($product_id);
			
			$attr_value=M('goods_attr')->where("id IN(".str_replace('-',',',$product['goods_attr_id']).")")->getField('attr_value',true);

			$_SESSION['cart']['goods'][$cartId]['cart_id']=$cartId;

			$_SESSION['cart']['goods'][$cartId]['goods_name']=$goods['goods_name'];

			$_SESSION['cart']['goods'][$cartId]['attr_value']="[".implode(',', $attr_value)."]";

			$_SESSION['cart']['goods'][$cartId]['gid']=$gid;

			$_SESSION['cart']['goods'][$cartId]['price']=$goods['goods_price'];

			$_SESSION['cart']['goods'][$cartId]['img']=$goods['goods_thumb'];

			$_SESSION['cart']['goods'][$cartId]['product_id']=$product_id;

			$_SESSION['cart']['goods'][$cartId]['num']=$num;

			$_SESSION['cart']['goods'][$cartId]['totalPrice']=$num*$_SESSION['cart']['goods'][$cartId]['price'];

		}

		$data=$this->countPrice();

		$_SESSION['cart']['totalPrice']=$data['totalPrice'];

		$_SESSION['cart']['totalNum']=$data['totalNum'];
		
		echo json_encode(array('state' => 1));exit;
	}
	/**
	 * 购物车唯一id
	 * @return [type] [description]
	 */
	private function getCartId(){

		return md5($_POST['gid'].'-'.$_POST['product_id']);
	}
	/**
	 * 更改购物车中货品数量
	 * @return [type] [description]
	 */
	public function changeNum(){
		// 购物车id
		$cart_id=Q('post.cart_id');
		//数量
		$num=Q('post.num');
		
		if (!$cart_id || !$num || !isset($_SESSION['cart']['goods'][$cart_id])) {
			$this->error("修改失败");

		}else{

			$_SESSION['cart']['goods'][$cart_id]['num']=$num;

			$data=$this->countPrice();

			$_SESSION['cart']['goods'][$cart_id]['totalPrice']=$num*$_SESSION['cart']['goods'][$cart_id]['price'];

			$_SESSION['cart']['totalPrice']=$data['totalPrice'];

			$_SESSION['cart']['totalNum']=$data['totalNum'];
			
			echo json_encode(array('status'=>'1','datas'=>$_SESSION['cart']));exit;
		}
	}
	/**
	 * 删除购物车中一件商品
	 * ######准备修改成异步（还未开始）
	 * @return [type] [description]
	 */
	public function del(){
		$cart_id=Q('cart_id',0,'trim');
		if (isset($_SESSION['cart']['goods'][$cart_id])) {

			unset($_SESSION['cart']['goods'][$cart_id]);
			
			$data=$this->countPrice();

			$_SESSION['cart']['totalPrice']=$data['totalPrice'];

			$_SESSION['cart']['totalNum']=$data['totalNum'];

			$data['status']=1;

			echo json_encode(array('data'=>$data));exit;
		}
	}
	/**
	 * 计算总价格
	 * @return [type] [description]
	 */
	public function countPrice(){
		//计算总价格
			$data['totalNum']=$data['totalPrice']=$data['i']=0;

			foreach ($_SESSION['cart']['goods'] as $g) {

				$data['i']+=1;

				$data['totalPrice']+=$g['num']*$g['price'];

				$data['totalNum']+=$g['num'];
			}
			return $data;
	}

	public function code(){
		$db=new Code();
		$db->show();
	}
}
?>